package com.nuonuo.middleware.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author duan
 * @version 1.0
 * @date 2023/12/28 15:23
 */
@Component
@Order
public class ConsumerHandler {

    private MessageConsumer consumer;

    public static final ArrayBlockingQueue queue = new ArrayBlockingQueue(8);
    public static final ArrayBlockingQueue<Runnable> workqueue = new ArrayBlockingQueue(8);
    public static ExecutorService executorService;

    @Autowired
    public ConsumerHandler() {
        ThreadFactory namedThreadFactory =
                new ThreadFactoryBuilder().setNameFormat("fpmp-thread-%d").build();
        executorService = new ThreadPoolExecutor(
                8,
                8,
                10L,
                TimeUnit.SECONDS,
                workqueue, namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
//        executorService.execute(new FpmpConsumerHandler());
        consumer = new MessageConsumer();
        consumer.start();
    }

//    @PostConstruct
//    public void init() {
//
//    }

//    public static void addQueue(String value) throws InterruptedException {
//        workqueue.put(new FpmpConsumerHandler());
//        queue.put(value);
//    }

    public static class FpmpConsumerHandler implements Runnable {

        private String value;

        public FpmpConsumerHandler(String value) {
            this.value = value;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + "##########" + value);
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
